package com.jn.news.ui;

import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.jn.news.R;
import com.jn.news.model.MedicineInfo;
import com.jn.news.utils.StatusBarUtils;
import com.jn.news.utils.ToastUtil;
import com.jn.news.viewmodel.MedicineSearchViewModel;
import com.jn.news.viewmodel.TianxingNewsListViewModel;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class MedicineSearchActivity extends AppCompatActivity {

    private ImageView ivBack;
    private EditText etMedicine;
    private TextView tvSearch;
    private TextView tvName;
    private MedicineSearchViewModel viewModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.with(this).init3(2, 102);
        setContentView(R.layout.activity_medicine_search);
        viewModel = new ViewModelProvider(this).get(MedicineSearchViewModel.class);
        initView();
        initData();
    }


    private void initView() {
        ivBack = findViewById(R.id.toolbar_left);
        etMedicine = findViewById(R.id.et_medicine_name);
        tvSearch = findViewById(R.id.tv_search);
        tvName = findViewById(R.id.tv_name);

    }

    private void initData() {
        ivBack.setOnClickListener(v -> finish());
        tvSearch.setOnClickListener(v -> {
            String medicineName = etMedicine.getText().toString().trim();
            if (medicineName.isEmpty()) {
                // 输入为空，显示错误提示
                etMedicine.setError("请输入药品名称");
            } else {
                // 输入不为空，进行搜索
                queryMedicine(medicineName);
            }
        });
    }

    private void queryMedicine(String medicineName) {
        viewModel.queryMedicine(medicineName)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(response -> {
                            if (response.getCode() == 200) {
                                handleData(response.getResult().getList().get(0));
                            } else {

                                ToastUtil.showToast(this, "请求失败: " + response.getMessage());
                            }
                        }
                        , throwable -> {
                            ToastUtil.showToast(this, "网络错误");
                        });
    }

    private void handleData(MedicineInfo medicineInfo) {
        // 处理字符串：替换【】为HTML加粗标签 + 换行
        String formatted = medicineInfo.getContent()
                .replaceAll("[\\t\\n\\r]", " ")                    // 去除制表符、换行符
                .replaceAll("\\s+", " ")                          // 合并多个空格为一个
                .trim();


// 设置到 TextView
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            tvName.setText(Html.fromHtml(formatted, Html.FROM_HTML_MODE_LEGACY));
        } else {
            tvName.setText(Html.fromHtml(formatted));
        }
        tvName.setText(medicineInfo.getContent());
    }
}